const { defineConfig } = require('@vue/cli-service');
// 文件复制插件
const CopyPlugin = require('copy-webpack-plugin');
// 压缩
const CompressionWebpackPlugin = require('compression-webpack-plugin');

// CDN外链，会插入到index.html中
// const cdn = {
//     // 开发环境
//     dev: {
//         css: ['https://cdn.bootcdn.net/ajax/libs/vant/3.6.1/index.min.css'],
//         js: [
//             'https://cdn.bootcdn.net/ajax/libs/vConsole/3.14.6/vconsole.min.js',
//             'https://cdn.bootcdn.net/ajax/libs/vue/3.2.38/vue.global.js',
//             'https://cdn.bootcdn.net/ajax/libs/vue-router/4.1.5/vue-router.global.js',
//             'https://cdn.bootcdn.net/ajax/libs/vuex/4.0.2/vuex.global.js',
//             'https://cdn.bootcdn.net/ajax/libs/vant/3.6.1/vant.min.js',
//             'https://cdn.bootcdn.net/ajax/libs/echarts/5.3.3/echarts.min.js'
//         ]
//     },
//     // 生产环境
//     build: {
//         css: [],
//         js: []
//     }
// };

module.exports = defineConfig({
    transpileDependencies: true,
    // 修改webpack配置
    configureWebpack: (config) => {
        // 不进行打包的模块
        config.externals = {
            AMap: 'AMap',
            Loca: 'Loca'
            // vue: 'Vue',
            // vuex: 'Vuex',
            // 'vue-router': 'VueRouter',
            // echarts: 'echarts',
            // vant: 'vant',
            // vconsole: 'VConsole'
        };
        // 拷贝static中的资源文件
        config.plugins.push(
            new CopyPlugin([
                {
                    from: 'static',
                    to: 'static',
                    toType: 'dir',
                    ignore: ['.DS_Store']
                }
            ]),
            new CompressionWebpackPlugin({
                test: /\.js$|\.html$|\.css/, //匹配文件名
                threshold: 10240, //对超过10k的数据进行压缩
                deleteOriginalAssets: false //是否删除原文件
            })
        );
    },
    chainWebpack: (config) => {
        config.plugins.delete('preload');
        // when there are many pages, it will cause too many meaningless requests
        config.plugins.delete('prefetch');

        // 注入cdn变量 (打包时会执行)
        // config.plugin('html').tap((args) => {
        //     args[0].cdn = cdn; // 配置cdn给插件
        //     return args;
        // });
        // 添加自定义参数cdn
        // config.plugin('html').tap((args) => {
        //     if (process.env.NODE_ENV === 'production') {
        //         args[0].cdn = cdn.build;
        //     } else {
        //         args[0].cdn = cdn.dev;
        //     }
        //     return args;
        // });
    },
    // 参考webpack-dev-serve
    devServer: {
        port: 9040,
        proxy: {
            '/dev': {
                target: 'http://10.0.101.93:31500/app/', //开发环境
                changeOrigin: true,
                pathRewrite: {
                    '^/dev': ''
                }
            },
            '/test': {
                target: 'https://api-saas-test.sscanmou.com', //测试环境
                changeOrigin: true,
                pathRewrite: {
                    '^/test': '/'
                }
            },
            '/product': {
                target: 'https://saas.sscanmou.com', //生产环境
                changeOrigin: true,
                pathRewrite: {
                    '^/product': '/apiV2/'
                }
            }
        }
    }
});
